# -*- encoding=utf8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import datetime
import time
from selenium.webdriver.support.wait import WebDriverWait

options = webdriver.ChromeOptions()
options.add_experimental_option('detach',True)
wd = webdriver.Chrome(options=options)

wd.implicitly_wait(5)

def switchToWindowByTitle(title):
    handles = wd.window_handles

    for handle in handles:
        wd.switch_to.window(handle)
        if wd.title == title:
            print(f"切换到{title}")
            return

wd.get("https://school.nicezhuanye.com/login/index.html#/loginMain?schoolID=25166186")

wd.maximize_window()

wd.find_element(By.XPATH,'//div[text()="账号密码"]').click()

wd.find_element(By.XPATH,'//input[@placeholder="请输入账号/手机号"]').send_keys("LingDeJinTou")

wd.find_element(By.XPATH,'//input[@placeholder="请输入密码"]').send_keys("aa123321")

wd.find_element(By.XPATH,'//span[text()="登 录"]').click()

if(wd.find_element(By.XPATH,'//div/screenshot[@title="应用平铺"]').is_displayed()):
    wd.find_element(By.XPATH, '//div/screenshot[@title="应用平铺"]').click()

wd.find_element(By.XPATH,'//div[text()="智能考务"]').click()

current_handle = wd.current_window_handle

handle = wd.window_handles

wd.switch_to.window(handle[-1])

print(current_handle)

title = wd.title

assert "智能考务" == title

ac = ActionChains(wd)

# ac.move_to_element(wd.find_element(By.XPATH,'//button/span[text()="知道了"]'))
#右击
# ac.context_click(wd.find_element(By.XPATH,'//button/span[text()="知道了"]'))
#双击
# ac.double_click(wd.find_element(By.XPATH,'//button/span[text()="知道了"]'))
# ac.click(wd.find_element(By.XPATH,'//button/span[text()="知道了"]'))
# ac.perform()

#普通点击无效时使用js方式
# wd.find_element(By.XPATH,'//button/span[text()="知道了"]').click()
#js注入方式点击
wd.execute_script("arguments[0].click()",wd.find_element(By.XPATH,'//button/span[text()="知道了"]'))

#滚动
# js = "window.scrollTo(0,10000)"
# js = "window.scrollTo(0,document.body.scrollHeight)"
# js = "var q=document.documentElement.scrollTop=2000"
# wd.execute_script(js)

# time = time.time()

time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

# wd.find_element(By.XPATH,'//span[text()="新建排考"]').click()
wd.execute_script("arguments[0].click()",wd.find_element(By.XPATH,'//span[text()="新建排考"]'))
wd.find_element(By.XPATH,'//input[@placeholder="请输入考试名称"]').send_keys(time_now)

wd.find_element(By.XPATH,'//div[@id="examScoreTypeId"]').click()
wd.find_element(By.XPATH,'//li[text()="月考"]').click()

# wd.find_element(By.XPATH,'//span[text()="确 定"]').click()
#显式等待并查找元素
el = WebDriverWait(wd,5,0.5).until(lambda x:x.find_element(By.XPATH,'//span[text()="确 定"]'))
wd.execute_script("arguments[0].click()",el)

switchToWindowByTitle("智能考务")

# wd.switch_to.window(wd.window_handles[-1])

wd.find_element(By.XPATH,'//div[@id="grade"]').click()
wd.find_element(By.XPATH,'//li[text()="高一（2022级）"]').click()
wd.execute_script("arguments[0].click()",wd.find_element(By.XPATH,'//span[text()="添 加"]'))

#截屏
wd.get_screenshot_as_file("error{}.png".format(time.strftime("%Y_%m_%d_%H_%M_%S")))